When a star is too small, don't draw it.
This is a somewhat hard thought, and I suggest that you get out pencil and paper and draw some stars attached to stars attached to stars. After a while it is clear that you want to stop drawing when the stars are too small to draw. Our applet is no different:
private void drawStar( int x, int y, int size ) { int endX, endY ; if ( size <= 2 ) return; // base case // Six lines radiating from (x,y) for ( int i = 0; i<6; i++ ) { endX = x + (int)(size*Math.cos( (2*Math.PI/6)*i )); endY = y - (int)(size*Math.sin( (2*Math.PI/6)*i )); graph.drawLine( x, y, endX, endY ); drawStar( endX, endY, size/3 ); } }
I picked the value "2" for the base case. Smaller values result in cluttered snowflakes.
In this code, if the if
statement followed the
for
statement, would everything be OK?